﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Buccaneer
{
    public class FileBrowser
    {
        public static ListView listView;

        public static void GetDrives(ref TreeView parent, ref ListView lv, string mainNodeName)
        {
            parent.Nodes.Add(new TreeNode(mainNodeName));

            parent.BeforeExpand += new TreeViewCancelEventHandler(parent_BeforeExpand);
            parent.BeforeSelect += new TreeViewCancelEventHandler(parent_BeforeSelect);

            listView = lv;

            TreeNodeCollection drives = parent.Nodes[0].Nodes;

            string[] drivesArray = Environment.GetLogicalDrives();

            foreach (string s in drivesArray)
            {
                string sx = s.Split(new string[] { "\\" }, StringSplitOptions.None)[0];
                try
                {
                    if (Directory.GetDirectories(s).Length != 0)
                        drives.Add(sx).Nodes.Add("nix");
                }
                catch (IOException)
                {
                    drives.Add(sx);
                }
                catch (UnauthorizedAccessException) { }
                drives[drives.Count - 1].Name = sx;
            }
        }

        static void parent_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            listView.Items.Clear();
            if (e.Node.Name != "")
                listView.Items.AddRange(GetFiles(e.Node.Name));
        }

        static void parent_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Text == "PC") return;
            e.Node.Nodes.Clear();
            e.Node.Nodes.AddRange(GetSubfolders(e.Node));
        }

        public static TreeNode[] GetSubfolders(TreeNode parent)
        {
            DirectoryInfo dir = new DirectoryInfo(parent.Name);

            DirectoryInfo[] subdirs = dir.GetDirectories();
            TreeNode[] retval = new TreeNode[subdirs.Length];

            for (int i = 0; i < subdirs.Length; i++)
            {
                retval[i] = new TreeNode(subdirs[i].Name);
                retval[i].Name = subdirs[i].FullName;
                try
                {
                    if (subdirs[i].GetDirectories().Length != 0)
                        retval[i].Nodes.Add("nix");
                }
                catch (UnauthorizedAccessException) { }
            }

            return retval;
        }

        public static ListViewItem[] GetFiles(string map)
        {
            DirectoryInfo dir;
            try { dir = new DirectoryInfo(map); }
            catch (ArgumentException) { return null; }

            DirectoryInfo[] subdirs = dir.GetDirectories();
            FileInfo[] files = dir.GetFiles();
            ListViewItem[] retval = new ListViewItem[subdirs.Length + files.Length];

            for (int i = 0; i < subdirs.Length; i++)
            {
                retval[i] = new ListViewItem(subdirs[i].Name);
                retval[i].Name = subdirs[i].FullName;
            }

            for (int i = subdirs.Length; i < files.Length + subdirs.Length; i++)
            {
                retval[i] = new ListViewItem(files[i - subdirs.Length].Name);
                retval[i].Name = files[i - subdirs.Length].FullName;
            }

            return retval;
        }
    }
}
